<template>
  <div class="app-container">

    <u-page title="临时导购管理">

      <div slot="header">
        <!--        <el-button type="primary" icon="el-icon-edit" @click="hdlOpen(newForm)">
          {{ textMap['create'] }}
        </el-button>-->
      </div>

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="部门" prop="dept">
            <el-input v-model="listQuery.dept" placeholder="关键词搜索" clearable />
          </el-form-item>
          <el-form-item label="商户" prop="merchantName">
            <el-input v-model="listQuery.merchantName" placeholder="关键词" clearable />
          </el-form-item>
          <!--          <el-form-item label="楼层" prop="floor">-->
          <!--            <mall-floor v-model="listQuery.floor" />-->
          <!--          </el-form-item>-->
          <el-form-item label="姓名" prop="name">
            <el-input v-model="listQuery.name" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="工号" prop="jobNo">
            <el-input v-model="listQuery.jobNo" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="listQuery.mobile" placeholder="关键词" clearable />
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-select v-model="listQuery.status" placeholder="请选择状态" class="status" clearable>
              <el-option value="" label="全部" />
              <el-option :value="0" label="待入职" />
              <el-option :value="1" label="正常" />
              <el-option :value="2" label="离职" />
              <el-option :value="3" label="锁定" />
              <el-option :value="4" label="拒绝" />
            </el-select>
          </el-form-item>
          <el-form-item label="入职时间">
            <u-date-time-picker v-model="searchDate" @change="changeSearchDate" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
            <el-button v-if="!purview" type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
            <!--            <el-button v-if="purview" type="primary" icon="el-icon-download" @click="handleGuideDownload">导出</el-button>-->
          </el-form-item>
          <el-form-item v-if="!purview">
            <el-dropdown>
              <el-button type="primary">
                批量操作<i class="el-icon-arrow-down el-icon--right" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :disabled="selectData.length===0" @click.native="hdlDel">批量删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlDetail(row)">详情</el-button>
              <el-button v-if="row.status !== 0 && row.status !== 4" type="text" @click="hdlEdit(row)">编辑</el-button>
              <el-button v-if="row.status !== 0 && row.status !== 4" type="text" @click="hdlCreate(row)">工牌</el-button>
              <el-button v-if="row.status === 1 " type="text" @click="hdlExtend(row)">更改时间</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>
    </u-page>
    <modal v-model="dialogFormVisible" :data="dataForm" />
    <edit v-model="dialogEditVisible" :data="editForm" @change="hdlList" />

    <u-dialog title="工牌生成" :show.sync="dialogBadgeVisible" width="410px" :footer="false">

      <div v-if="data.badgeVisible" ref="capture" class="badge">
        <span class="company">{{ data.company }}</span>
        <div class="avatar-placeholder">
          <img class="avatar" :src="data.avatarUrl" :fit="data.avatarfit" alt="头像">
        </div>
        <span class="name">{{ data.name }}</span>
        <img class="corner-image" :src="data.cornerImage" :fit="data.fit" alt="角落图片">
      </div>
      <el-button type="primary" style="margin-top: 20px;" @click="saveAsImage()">保存为图片</el-button>
    </u-dialog>

    <extend v-model="dialogExtendVisible" :data="dataForm" @change="hdlList"/>
  </div>
</template>
<script>
import modal from '@/views/plugins/guide/temporary/components/modal.vue'
import MallFloor from '@/components/mall/floor.vue'
import edit from '@/views/plugins/guide/temporary/components/edit.vue'
import html2canvas from 'html2canvas'
import extend from '@/views/plugins/guide/temporary/components/extend.vue'

export default {
  components: { edit, modal, MallFloor, extend },
  data() {
    return {
      api: this.$u.api.PluginsGuideTemporary,
      // 表格
      columns: [
        {
          prop: 'status',
          label: '状态',
          render: (h, params) => {
            const { status } = params.row
            if (status === 0) {
              return h('el-tag', { props: { type: 'warning' }}, '待入职')
            } else if (status === 1) {
              return h('el-tag', { props: { type: 'success' }}, '正常')
            } else if (status === 2) {
              return h('el-tag', { props: { type: 'info' }}, '离职')
            } else if (status === 3) {
              return h('el-tag', { props: { type: 'danger' }}, '锁定')
            } else if (status === 4) {
              return h('el-tag', { props: { type: 'danger' }}, '拒绝')
            }
          }
        },
        {
          prop: 'jobNo',
          label: '工号'
        },
        {
          prop: 'name',
          label: '姓名'
        },
        {
          prop: 'mobile',
          label: '手机号'
        },
        {
          prop: 'dept',
          label: '部门'
        },
        {
          prop: 'enterpriseName',
          label: '公司'
        },
        {
          prop: 'merchantName',
          label: '商户'
        },
        {
          prop: 'floor',
          label: '楼层'
        },
        {
          prop: 'createTime',
          label: '入职时间'
        },
        {
          prop: 'startTime',
          label: '开始时间'
        },
        {
          prop: 'days',
          label: '支援天数'
        },
        {
          prop: 'note',
          label: '备注'
        }
      ],

      list: [],
      listLoading: false,
      listOptions: {
        mutiSelect: true, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        endTime: '',
        name: '',
        mobile: '',
        status: ''
      },
      purview: false,
      dialogFormVisible: false,
      dialogEditVisible: false,
      editForm: {},
      dialogBadgeVisible: false,
      // 表格
      data: {
        avatarfit: '',
        cornerImagefit: '',
        name: '',
        company: '',
        cornerImage: 'https://csms.chiconysquare.net/assets/logo/brandlogo.png',
        badgeVisible: true,
        avatarUrl: ''
      },
      dialogExtendVisible: false
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    hdlExtend(row) {
      this.dataForm = row
      this.dialogExtendVisible = true
    },
    saveAsImage() {
      const element = this.$refs.capture
      html2canvas(element).then((canvas) => {
        const link = document.createElement('a')
        link.href = canvas.toDataURL('image/png')
        link.download = 'badge-image.png'
        link.click()
      })
    },
    hdlCreate(row) {
      console.log(row)
      this.dialogBadgeVisible = true
      // this.data = row
      this.data.avatarfit = 'cover'
      this.data.cornerImagefit = ''
      this.data.name = row.name
      this.data.company = row.merchantName
      this.data.cornerImage = 'https://csms.chiconysquare.net/assets/logo/brandlogo.png'
      this.data.avatarUrl = 'https://csms.chiconysquare.net/' + row.avatar

      // this.data.avatarUrl = ' http://j.dsik.cn/' + row.avatar
      this.data.badgeVisible = true
    },
    hdlEdit(row) {
      this.editForm = row
      this.dialogEditVisible = true
    },
    hdlDetail(row) {
      this.dataForm = row
      this.dialogFormVisible = true
    },
    /**
     * 获取列表数据
     */
    hdlList() {
      const self = this
      // 打开加载状态
      self.listLoading = true
      // 查询分页数据
      self.api.list(self.listQuery).then(res => {
        // 获取分页列表数据
        self.list = res.data.list
        // 获取分页数据,总页数
        self.listQuery.totalCount = res.data.totalCount
        self.purview = res.data.purview
        // 关闭加载状态
        setTimeout(() => {
          self.listLoading = false
        }, 0.5 * 1000)
      })
    },

    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'province', item.province + item.city + item.area + item.address)
        this.$set(item, 'status', item.status === 0 ? '待入职' : item.status === 1 ? '正常' : item.status === 2 ? '离职' : item.status === 3 ? '锁定' : item.status === 4 ? '拒绝' : '')
      })
      // 表头&表数据
      // const tHeader = ['工号', '姓名', '民族', '手机', '籍贯', '生日', '身份证', '性别', '身份证有效期', '年龄', '户籍地址', '邮箱', '最高学历', '入职时间', '现住地址', '餐卡', '每月餐费标准', '状态', '备注', '离职时间', '离职事由', '紧急联络与本人关系', '联络人', '联络电话', '公司名称', '公司编码', '合同编号', '商户名称', '商户编码', '商户楼层', '身份证有效期', '健康证有效期']
      // const filterVal = ['jobNo', 'name', 'nationality', 'mobile', 'bornPlace', 'birthday', 'idCard', 'sex', 'idCardValidity', 'age', 'bornAddress', 'email', 'highestDegree', 'createTime', 'province', 'mealCardNo', 'mealCost', 'status', 'note', 'endTime', 'endContent', 'emergencyContactRelation', 'emergencyName', 'emergencyMobile', 'enterpriseName', 'enterpriseNo', 'contractNo', 'merchantName', 'merchantNo', 'floor', 'idCardValidity', 'healthCertificateValidity']

      const tHeader = [
        '工号',
        '姓名',
        '民族',
        '手机',
        '籍贯',
        '生日',
        '身份证',
        '性别',
        '身份证有效期',
        '年龄',
        '户籍地址',
        '入职时间',
        '离职时间',
        '状态',
        '备注',
        '公司名称',
        '公司编码',
        '合同编号',
        '商户名称',
        '商户编码',
        '商户楼层'
      ]

      const filterVal = [
        'jobNo',
        'name',
        'nationality',
        'mobile',
        'bornPlace',
        'birthday',
        'idCard',
        'sex',
        'idCardValidity',
        'age',
        'bornAddress',
        'createTime', // 保留
        'endTime',
        'status',
        'note', // 保留

        'enterpriseName',
        'enterpriseNo',
        'contractNo',
        'merchantName',
        'merchantNo',
        'floor'
      ]

      const data = list.map(v => filterVal.map(j => { return v[j] }))
      this.$u.exports.excel('临时导购管理', tHeader, data)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container{

  ::v-deep {
    .el-descriptions__title {
      font-size: 14px;
      font-weight: bold;
    }
  }

  & >>> .el-drawer__body {
    overflow: auto;
  }

  .uploader{

    .thumb{
      display: inline-block;
      text-align: center;
      outline: none;
      background-color: #fbfdff;
      border: 1px dashed #c0ccda;
      border-radius: 6px;
      box-sizing: border-box;
      width: 120px;
      height: 120px;
      cursor: pointer;
      line-height: 120px;
      vertical-align: top;

      ::v-deep {
        .el-image {
          width: 100%;
          height: 100%;
          display: block;
          border-radius: 6px;
        }

        img{
          width: 100%;
          height: 100%;
          display: block;
          border-radius: 6px;
        }
      }
    }
  }

}

.badge {
  border: 1px solid #000;
  border-radius: 5px;
  width: 371px;
  height: 602px;
  //padding: 20px;
  text-align: center;
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  position: relative;
}
.company {
  font-weight: bold;
  margin-top: 50px; /* 下移50px */

  display: -webkit-box; /* 使其成为弹性盒子 */
  -webkit-box-orient: vertical; /* 设置主轴方向为纵向 */
  -webkit-line-clamp: 2; /* 限制显示的行数为 2 */
  overflow: hidden; /* 隐藏多余内容 */
  transition: font-size 0.3s ease; /* 字体大小变化的过渡效果 */
  font-size: 40px; /* 默认字体大小 */
  color: black;
}

.name {
  font-size: 55px; /* 放大一倍 */
  font-weight: bold;
  color: black;
}
.avatar-placeholder {
  width: 245px; /* 椭圆形宽度 */
  height: 280px; /* 椭圆形高度 */
  border: 2px solid #000;
  border-radius: 50%; /* 椭圆形 */
  margin: 20px auto;
  position: relative;
  overflow: hidden;
}
.avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  position: absolute;
  top: 0;
  left: 0;
}
.corner-image {
  position: absolute;
  width: 147px;
  height: 49px;
  top: 20px;
  right: 20px;
  object-fit: cover; /* 确保图像适应框 */
}
</style>
